<?php
namespace User\Api;

use Org\Util\OAuthV2;
use User\Model\UserModel;
class UserApi{
    
    private $key = '';
    private $secret = '';
    private $url = '';
    private $model = '';
    public function __construct() {
        $login_info = C('A_LOGIN_INFO');
        $this->key        = $login_info['key'];
        $this->secret     = $login_info['secret'];
        $this->url        = $login_info['url'];
        $this->model      = new UserModel();
    }


    /**
     * 用户登录
     * @param string $username 用户名
     * @param string $password 密码
     * @param int $remember 是否记住密码 保留
     * @param int $is_third 是1否0是第三方登录
     * @return 登录
     */
    public function login($username, $password, $remember, $is_third){
        if($is_third){
            return $this->third_login($username, $password);
        }else{
            return $this->model->login($username, $password, $remember);
        }
    }

    /**
     * 第三方老A登录
     * @param type $username  用户名
     * @param type $password  密码
     * @return 返回第三方用户id或错误码
     */
    public function third_login($username, $password){

        //获取taken
        $oauth = new OAuthV2($this->key, $this->secret);
        $tokenresponse = $oauth->getaccesstoken($this->url,'POST');
        $token = json_decode($tokenresponse,true);
        
        if ( is_array($token) && $token['errorCode']==0 ){
            $accessToken = $token['accessToken'];
            $oauth->access_token = $accessToken;
            //第三方验证登录
            $user_login_validate = $oauth->getvalidateuser($this->url, 'POST', $accessToken, $username, $password);
            $user = json_decode($user_login_validate, true);

            //第三方登录成功
            if(isset($user) && $user['errorCode'] == 0){
                $member = M("User_third")->where(array('openid' => $user['openId']))->find();
                //判断第三方用户是否已存在数据表中
                if($member){
                    //返回第三方用户id
                    return array(
                        'id'        => $member['id'],
                        'user_type' => $user['userType'],
                        'rank' => $user['userRank'],
                    );
                }else{
                    //在第三方登陆表添加记录
                    $third_id = M('User_third')->add(array(
                        'openid'    => $user['openId'],
                    ));
                    return array(
                        'id'        => $third_id,
                        'realname'  => $user['realname'],
                        'user_type' => $user['userType'],
                        'rank' => $user['userRank'],
                    );
                }
            }
            return array(                   //用户名或密码错误
                'id'        => -2,
            ); 
        }
        return array(                       //连接第三方错误
            'id'        => -3,
            'msg'       => $user['errorMessage'],
        );      
    }    
    
    /**
     * 注册
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     * @param  [type] $mobile   [description]
     * @return [type]           [description]
     */
    public function register($username, $password, $mobile){
        return $this->model->register($username, $password, $mobile);
    }
    
    /**
     * 注册到老A
     * @param type $username
     * @param type $password
     * @param type $mobile
     * @return type
     */
    public function thirdRegister($username, $password, $mobile){
        
        //获取taken
        $oauth = new OAuthV2($this->key, $this->secret);
        $tokenresponse = $oauth->getaccesstoken($this->url,'POST');
        $token = json_decode($tokenresponse,true);
        
        if ( is_array($token) && $token['errorCode']==0 ){
            $accessToken = $token['accessToken'];
            $oauth->access_token = $accessToken;
            $registerIp = get_client_ip();
            $oo = $oauth->adduser($this->url, 'POST', $accessToken, $username, $password, $mobile, $registerIp);
            $user = json_decode($oo, true);
            if($user['errorCode'] == 0){
                $id = M('User_third')->add(array('openid' => $user['openId']));
                return array(
                    'code' => $id,
                );
            }else{
                return array(
                    'code'  => 0,
                    'msg'   => $user['errorMessage'],
                );
            }
        }
    }
}